//index.js
//获取应用实例
const app = getApp()
import Share from "../../lib/js/share.js";
var QQMapWX = require('../../lib/js/qqmap-wx-jssdk.js');
const font = require('../../lib/js/font.js')
var qqmapsdk;
Page({
  data: {
    height: app.globalData.height,
    titleBarHeight: app.globalData.titleBarHeight,
    // 组件所需的参数
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '首页', //导航栏 中间的标题
    },
    navHeight: app.globalData.navHeight,
    keyword: "",
    pageNum: 1,
    pageSize:10,
    isSending: false, // 是否正在加载
    isGetMore: true, // 是否还有更多
    list: [],
    token: "",
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    var that=this
 
  },


  // /**
  // * 用户点击右上角分享
  // */
  // onShareAppMessage: function () {
  //   let that = this;
  //   return {
  //     title: that.data.shareInfo.title,
  //     path: '/pages/index/index?share=true&spread_code=' + that.data.spread_code,
  //     imageUrl: that.data.shareInfo.icon,
  //     success: function (res) {
  //       wx.showToast({
  //         title: "已转发",
  //         icon: 'none',
  //         duration: 2000
  //       })
  //     }
  //   }
  // },
  
  onPullDownRefresh() {
    let that = this;
    that.setData({
      pageNum: 1,
      pageSize: 10,
      isSending: false, // 是否正在加载
      isGetMore: true, // 是否还有更多
      list: [],
      noData: false,
    })
    that.getList();
    wx.stopPullDownRefresh(); //为停止当前页面下拉刷新

  },

  onLoad: function (options) {
    let that = this;
    font.loadFont(); //下载字体
   
  },

  onShow: function () {
    let that = this
    let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ""
    that.setData({
      token: token
    })
    console.log(token)
    if (!that.data.token) {
      wx.navigateTo({
        url: '/packageD/pages/authorize/authorize'
      })
      return
    } 
    that.setData({
      pageNum: 1,
      pageSize: 10,
      isSending: false, // 是否正在加载
      isGetMore: true, // 是否还有更多
      list: [],
      noData: false,
    })
    that.getList()
  },
  // 输入
  onInputs(event) {
    const value = event.detail.value;
    this.setData({ keyword: value });
  },
   //搜索
  search(){
    let that=this;
    that.setData({
      pageNum: 1,
      pageSize: 10,
      isSending: false, // 是否正在加载
      isGetMore: true, // 是否还有更多
      list: [],
      noData: false,
    })
    that.getList();
  },
  close(){
    console.log(23424)
    this.setData({ keyword: '' });
    let that = this;
    that.setData({
      pageNum: 1,
      pageSize: 10,
      isSending: false, // 是否正在加载
      isGetMore: true, // 是否还有更多
      list: [],
      noData: false,
    })
    this.getList();
  },
  getList() {
    let that = this;
    let param = {
      pageNum: that.data.pageNum,
      pageSize: that.data.pageSize,
      model: that.data.keyword
    };
    if (that.data.isSending || !that.data.isGetMore) {
      return false;
    }
    that.setData({
      isSending: true,
    })
      app.fly.post('/ma/box/list', param)
      .then(function (res) {

        if (res.code === 0) {
        
          that.data.pageNum++;
          that.setData({
            list: that.data.list.concat(res.data.list),
            pageNum: that.data.pageNum,
          })
          if (that.data.list.length >= res.data.total) {
            that.data.isGetMore = false;
          }
          that.data.isSending = false;
          that.setData({
            pageNum: that.data.pageNum,
            isSending: that.data.isSending,
            isGetMore: that.data.isGetMore,
          })
          if(that.data.list.length<=0){
            that.setData({
              noData: true
            })
          }else{
            that.setData({
              noData: false
            })
          }
        } else {
          that.data.isSending = false;
          wx.showToast({
            title: '失败！联系客服',
            icon: 'none',
            duration: 2000
          })
        }
      }, function (res) {
        console.log(res);
      });

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
},
  toDetail(e){
    if (!this.data.token) {
      wx.navigateTo({
        url: '/packageD/pages/authorize/authorize'
      })
      return
    } 
    wx.navigateTo({
      url: '/packageA/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  }

})
